package com.pangolin.pangolinim.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.ImageUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class SearchUserActivity extends BaseActivity {
    private UIContact me;

    private LinearLayout mLlToolbarSearch;
    private TextView mToolbarTitle;
    private EditText mEtSearchContent;
    private RelativeLayout mRlNoResultTip;
    private LinearLayout mLlSearch;
    private TextView mTvMsg;

    @Override
    protected void init(){
        String strContact = getIntent().getStringExtra("param1");
        if(strContact != null){
            me = GsonUtil.fromJson(strContact , UIContact.class);
            Logger.d("contact:%d|%s" , me.getUid() , me.getName());
        }
    }

    @Override
    protected void initView(){
        mToolbarTitle = findViewById(R.id.tvToolbarTitle);
        mLlToolbarSearch = findViewById(R.id.llToolbarSearch);
        mEtSearchContent = findViewById(R.id.etSearchContent);
        mRlNoResultTip = findViewById(R.id.rlNoResultTip);
        mLlSearch = findViewById(R.id.llSearch);
        mTvMsg = findViewById(R.id.tvMsg);

        mToolbarTitle.setVisibility(View.GONE);
        mLlToolbarSearch.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initListener(){
        mEtSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String content = mEtSearchContent.getText().toString().trim();
                mRlNoResultTip.setVisibility(View.GONE);
                if (content.length() > 0) {
                    mLlSearch.setVisibility(View.VISIBLE);
                    mTvMsg.setText(content);
                } else {
                    mLlSearch.setVisibility(View.GONE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //点击搜索用户响应
        mLlSearch.setOnClickListener(v -> {
            String content = mEtSearchContent.getText().toString().trim();

            if (TextUtils.isEmpty(content)) {
                UIUtil.showToast(UIUtil.getString(R.string.content_no_empty));
                return;
            }
            if(!TextUtils.isDigitsOnly(content)){
                UIUtil.showToast(UIUtil.getString(R.string.content_digitsonly));
                return;
            }
            if(Integer.parseInt(content) == me.getUid()){
                UIUtil.showToast(UIUtil.getString(R.string.content_notme));
                return;
            }

            showWaitingDialog(UIUtil.getString(R.string.please_wait));
            //发送搜索用户请求
            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_FINDUSER);
            brocastMap.put("friend_uid" , Integer.parseInt(content));
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            BroadcastSender(bundleUtil.getBundle());
            //hideWaitingDialog();

        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA) {
            if (bundleUtil.getData() != null) {
                Map<String, Object> resMap = bundleUtil.getData();
                Logger.d(resMap);
                if ((int) resMap.get("type") == BroadcastMsg.DATA_CORE_FINDUSER) {    //搜索联系人请求返回
                    if((int ) resMap.get("result") == 0){
                        UIContact contact = new UIContact();
                        contact.setUid((int)resMap.get("uid"));
                        contact.setName((String)resMap.get("name"));
                        contact.setHeader((String) resMap.get("headimg"));
                        File file = new File(getExternalFilesDir(String.valueOf(me.getUid())) , contact.getHeader());
                        if(file.exists() && file.isFile()){
                            contact.setBitHeader(ImageUtil.getBitmapFromFile(this , file , 120 , 120));
                        }
                        jumpToActivity(UserInfoActivity.class , GsonUtil.toJson(me) , GsonUtil.toJson(contact));
                        hideWaitingDialog();
                        finish();
                    }else{
                        hideWaitingDialog();
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_search_user;
    }
}